//
//  ZYChangeLanguage.m
//  YiLin
//
//  Created by Zyark on 2018/5/29.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYChangeLanguage.h"

#define LocalLanguageKey @"AppleLanguages"
@implementation ZYChangeLanguage

static NSBundle *bundle = nil;

+ ( NSBundle * )bundle{
    return bundle;
}

DEFINE_SINGLETON_IMPLEMENTATION(ZYChangeLanguage)

- (void)setUp {
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
//    NSArray *preferredLanguages = [NSLocale preferredLanguages];
    NSArray *array = [def valueForKey:LocalLanguageKey];
    NSString *currLanguage = [array lastObject];
    if(![ZYConfig isInitLanguage]){
        currLanguage = @"zh-Hans";
    }else{
        if(!currLanguage){
            currLanguage = @"en";
        }
        if ([currLanguage hasPrefix:@"en"]) {
            currLanguage = @"en";
        }else if ([currLanguage hasPrefix:@"zh"]) {
            currLanguage = @"zh-Hans";
        }else currLanguage = @"en";
    }
    [def setValue:[NSArray arrayWithObjects:currLanguage, nil] forKey:LocalLanguageKey];
    [def synchronize];
    //获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:currLanguage ofType:@"lproj"];
    bundle = [NSBundle bundleWithPath:path];//生成bundle
}

//首次加载的时候先检测语言是否存在

+(void)initUserLanguage{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSArray *array = [def valueForKey:LocalLanguageKey];
    NSString *currLanguage = [array lastObject];
    if(![ZYConfig isInitLanguage]){
        currLanguage = @"en";
    }else{
        if(!currLanguage){
            currLanguage = @"en";
        }
        if ([currLanguage hasPrefix:@"en"]) {
            currLanguage = @"en";
        }else if ([currLanguage hasPrefix:@"zh"]) {
            currLanguage = @"zh-Hans";
        }else currLanguage = @"en";
    }
    [def setValue:[NSArray arrayWithObjects:currLanguage, nil] forKey:LocalLanguageKey];
    [def synchronize];
    //获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:currLanguage ofType:@"lproj"];
    bundle = [NSBundle bundleWithPath:path];//生成bundle
}


-(NSString *)getStringByKey:(NSString *)key{
    return [bundle localizedStringForKey:key value:@"" table:LC_TABLE];
//    return NSLocalizedString(key, @"");
}


//获取当前语言

-(NSString *)userLanguage{
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSArray *array = [def valueForKey:LocalLanguageKey];
    NSString *language = [array lastObject];
    return language;
}


//设置语言
-(void)setUserlanguage:(NSString *)language{
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *array = [userDefaults valueForKey:LocalLanguageKey];
    NSString *currLanguage = [array lastObject];
    if ([currLanguage isEqualToString:language]) {
        return;
    }
    [userDefaults setValue:[NSArray arrayWithObjects:language, nil] forKey:LocalLanguageKey];
    [userDefaults synchronize];
    NSString *path = [[NSBundle mainBundle] pathForResource:language ofType:@"lproj"];
    bundle = [NSBundle bundleWithPath:path];
}
@end
